//
// Created by v on 24-5-13.
//

#include "cm_protocol_convert_impl_general.h"
#include "cm_message_converter.h"
#include "utils/cm_utils.h"

CMProtocolConvertImplGeneral::CMProtocolConvertImplGeneral() {}
CMProtocolConvertImplGeneral::~CMProtocolConvertImplGeneral() {}

std::string CMProtocolConvertImplGeneral::GenerateDeviceBroadInfo(
    CMDeviceBroadInfo &broadInfo)
{
    CMMessageConverterJson converterJson;
    converterJson.setString("id", NULL, broadInfo.id);
    converterJson.setString("name", NULL, broadInfo.name);
    return converterJson.generateString();
}

int CMProtocolConvertImplGeneral::ParseDeviceBroadInfo(
    std::string &data, CMDeviceBroadInfo &broadInfo)
{
    CMMessageConverterJson converterJson;
    int ret = converterJson.load(data);
    if (ret < 0)
    {
        return -2;
    }

    cm_strncpy(broadInfo.name, converterJson.getString("name", NULL).c_str(),
               sizeof(broadInfo.name));
    cm_strncpy(broadInfo.id, converterJson.getString("id", NULL).c_str(),
               sizeof(broadInfo.id));
    return 0;
}

int CMProtocolConvertImplGeneral::ParseDeviceBroadInfo(
    const char *data, CMDeviceBroadInfo &broadInfo)
{
    std::string d(data);
    return ParseDeviceBroadInfo(d, broadInfo);
}
